package com.yzd.learnjava.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.yzd.learnjava.util.Print;

public class StoreCADState {
	public static void main(String[] args) throws Exception, IOException {
		List<Class<? extends Shape>> shapeTypes = new ArrayList<Class<? extends Shape>>();
		shapeTypes.add(Circle.class);
		shapeTypes.add(Squar.class);
		shapeTypes.add(Line.class);
		
		List<Shape> shapes = new ArrayList<Shape>();
		for(int i = 0 ; i < 10; i++) {
			shapes.add(Shape.randomFactory());
		}
		
		for(int i = 0 ; i < 10; i++) {
			((Shape)shapes.get(i)).setColor(Shape.GREEN);
		}
		
		ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("CADState.out"));
		
		os.writeObject(shapeTypes);
		Line.serialzeStaticState(os);
		os.writeObject(shapes);
		
		Print.print(shapes);
	}
}
abstract class Shape implements Serializable{
	public static final int RED = 1 ;
	public static final int BLUE = 2 ;
	public static final int GREEN = 3 ;
	private int xPos;
	private int yPos;
	private int dimension;
	private static Random random = new Random(47);
	private static int counter = 0;
	public abstract void setColor(int newColor);
	public abstract int getColor();
	public Shape(int xVal, int yVal, int dim) {
		this.xPos = xVal;
		this.yPos = yPos;
		this.dimension = dim;
	}
	
	public String toString() {
		return getClass() + "  color[" + getColor() + "] xPos[" + xPos + "] yPos[" + yPos + "] dim[" + dimension + "]\n";
	}
	
	public static Shape randomFactory() {
		int xVal = random.nextInt(100);
		int yVal = random.nextInt(100);
		int dim = random.nextInt(100);
		switch (counter++ % 3) {
			default:
			case 0:
				return new Circle(xVal, yVal, dim);
			case 1:
				return new Squar(xVal, yVal, dim);
			case 2:
				return new Line(xVal, yVal, dim);
			
		}
	}
	
}
class Circle extends Shape {
	private static int color = Shape.RED;
	public Circle(int x , int y , int d) {
		super(x, y, d);
	}
	public void setColor(int newColor) {
		color = newColor;
	}
	
	public int getColor() {
		return color;
	}
}

class Squar extends Shape {
	private static int color ;
	public Squar(int x , int y , int d) {
		super(x, y, d);
		color = RED;
	}
	public void setColor(int newColor) {
		color = newColor;
	}
	
	public int getColor() {
		return color;
	}
}

class Line extends Shape {
	private static int color = RED ;
	public Line(int x , int y , int d) {
		super(x, y, d);
	}
	public void setColor(int newColor) {
		color = newColor;
	}
	
	public int getColor() {
		return color;
	}
	
	public static void serialzeStaticState(ObjectOutputStream os) throws IOException {
		os.writeInt(color);
	}
	
	public static void deserialzeStaticState(ObjectInputStream os) throws IOException {
		color = os.readInt();
	}
}